/*
 * RMI demo
 * Spring 2013 TCSS 558
 * Daniel M. Zimmerman
 */

package rmi;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

/**
 * A client of the remote hashtable. It clears the hashtable, makes 
 * sure it is actually clear, and then exits.
 * 
 * @author Daniel M. Zimmerman
 * @version Spring 2013
 */
public final class ClearClient
{
  /**
   * Private constructor to prevent instantiation.
   */
  private ClearClient()
  {
    // do nothing
  }
  
  /**
   * The main method. The first command line argument is the hostname
   * of the registry; the second is the port number of the registry;
   * all others are ignored.
   * 
   * @param the_args Used as described in the method description.
   */
  public static void main(final String[] the_args)
  {
    try
    {
      // get a reference to the remote map
     
      final String host = the_args[0];
      final int port = Integer.parseInt(the_args[1]);
      final Registry registry = LocateRegistry.getRegistry(host, port);
      final RemoteMap map =
        (RemoteMap) registry.lookup("RemoteMap");
      
      // clear the map
      
      map.clear();
      
      // demonstrate that it is cleared
      
      System.out.println("0 is mapped to " + map.get("0"));
      System.out.println("1 is mapped to " + map.get("1"));
      System.out.println("2 is mapped to " + map.get("2"));
      System.out.println("3 is mapped to " + map.get("3"));
    }
    catch (final RemoteException e)
    {
      System.err.println("A problem occurred: " + e);
      System.exit(1);
    }
    catch (final NotBoundException e)
    {
      System.err.println("Could not find RMI server.");
      System.exit(1);
    }
  }
}
